package com.hddz.comlib.util;

import java.util.ArrayList;
import java.util.regex.Pattern;

import com.hddz.comlib.PrintSerialHelper;

public class SpeedataPrintUtility {
	private byte[] cmdS2;
	private PrintSerialHelper mPrintSerialHelper;

	public SpeedataPrintUtility() {
		mPrintSerialHelper = new PrintSerialHelper();
	}

	public Boolean openPrint() {
		return mPrintSerialHelper.startPrintSerial();
	}

	public Boolean closePrint() {
		return mPrintSerialHelper.stopPrintSerial();
	}

	private ArrayList<String> getStrLen(String mInfo) {
		int len = 0, index = 0;
		ArrayList<String> result = new ArrayList<String>();
		char[] chars = mInfo.toCharArray();
		len = 0;
		for (int j = 0; j < chars.length; j++) {
			if (chars[j] >= 0x4E00 && chars[j] <= 0x9FCC) {
				len += 2;
			} else {
				len += 1;
			}
			index += 1;
			if (len >= 32) {
				result.add(mInfo.substring(0, index));
				mInfo = mInfo.substring(index, mInfo.length());
				index = 0;
				len = 0;
			}
		}
		if (mInfo.length() > 0) {
			result.add(mInfo);
		}
		return result;
	}

	private void printStr(String mInfo) {
		byte[] data1;
		byte[] data2;
		data2 = new byte[1];
		data1 = null;
		data2[0] = 0x0a;
		try {
			data1 = mInfo.getBytes("GB2312");
			cmdS2 = new byte[data1.length + 1];
			System.arraycopy(data1, 0, cmdS2, 0, data1.length);
			System.arraycopy(data2, 0, cmdS2, data1.length, data2.length);
			mPrintSerialHelper.print(cmdS2);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void printPage(String mInfo, Boolean isUp, int fontSize, int header, int ender) {
		byte[] cmdS1;
		byte[] cmdS2;
		byte[] cmdS3;
		Pattern pattern = Pattern.compile("[\\u4E00-\\u9FCC]+");
		int len = 0;
		String[] strs = mInfo.split("\n");

		try {
			cmdS1 = new byte[3];
			cmdS1[0] = 0x1B;
			cmdS1[1] = 0x7B;
			cmdS1[2] = (byte) (isUp ? 0x01 : 0x00); // 0正常（默认）1颠倒
			mPrintSerialHelper.print(cmdS1);

			cmdS3 = new byte[6];
			cmdS3[0] = 0x1B;
			cmdS3[1] = 0x33;
			cmdS3[2] = (byte) 0; // 设置行间距
			cmdS3[3] = 0x1B;
			cmdS3[4] = 0x64;
			cmdS3[5] = (byte) header; // 设置走动行数
			mPrintSerialHelper.print(cmdS3);

			Thread.sleep(100);

			cmdS1[0] = 0x1B;
			cmdS1[1] = 0x33;
			cmdS1[2] = (byte) fontSize; // 设置行间距
			mPrintSerialHelper.print(cmdS1);

			for (int i = 0; i < strs.length; i++) {
				ArrayList<String> strparts = getStrLen(strs[i]);
				if (strparts.size() == 1 || strparts.size() == 0) {
					printStr(strs[i]);
				} else {
					Thread.sleep(100);
					cmdS1[0] = 0x1B;
					cmdS1[1] = 0x33;
					cmdS1[2] = (byte) 1; // 设置行间距
					mPrintSerialHelper.print(cmdS1);
					for (String s : strparts) {
						printStr(s);
					}
					Thread.sleep(100);
					cmdS1[0] = 0x1B;
					cmdS1[1] = 0x33;
					cmdS1[2] = (byte) fontSize; // 设置行间距
					mPrintSerialHelper.print(cmdS1);
				}
			}

			Thread.sleep(100);

			cmdS3[0] = 0x1B;
			cmdS3[1] = 0x33;
			cmdS3[2] = (byte) 0; // 设置行间距
			cmdS3[3] = 0x1B;
			cmdS3[4] = 0x64;
			cmdS3[5] = (byte) ender; // 设置走动行数
			mPrintSerialHelper.print(cmdS3);
		} catch (Exception ex) {
		}
	}
}
